package cn.dubito.springboot.learning.controller;

import cn.dubito.springboot.learning.converter.OrderParamToOrderDto;
import cn.dubito.springboot.learning.dto.OrderDTO;
import cn.dubito.springboot.learning.enums.ResultEnum;
import cn.dubito.springboot.learning.exception.SellException;
import cn.dubito.springboot.learning.param.OrderParam;
import cn.dubito.springboot.learning.service.BuyerService;
import cn.dubito.springboot.learning.service.OrderService;
import cn.dubito.springboot.learning.utils.ResultUtil;
import cn.dubito.springboot.learning.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liliang
 * @description:
 * @date 2018/1/14.
 */
@Slf4j
@RequestMapping("/buyer/order")
@RestController
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    /**
     * 创建订单
     *
     * @param orderParam
     * @param bindingResult
     * @return
     */
    @PostMapping("/create")
    public ResultVO createOrder(@Valid OrderParam orderParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("[创建订单] 参数不正确,orderParam={}", orderParam);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderParamToOrderDto.convert(orderParam);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            log.error("[创建订单] 购物车为空,无法创建订单 orderDetailList={}", orderDTO.getOrderDetailList());
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO createResult = orderService.createOrder(orderDTO);
        Map<String, String> map = new HashMap<>();
        map.put("orderId", createResult.getOrderId());
        return ResultUtil.success(map);
    }

    @GetMapping("list")
    public ResultVO list(@RequestParam("openid") String openid,
                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                         @RequestParam(value = "size", defaultValue = "20") Integer size) {
        if (StringUtils.isEmpty(openid)) {
            log.error("[获取订单列表] openid不能为空,openid={}", openid);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(page, size);
        Page<OrderDTO> orderDTOPage = orderService.findList(openid, pageRequest);
        return ResultUtil.success(orderDTOPage.getContent());
    }

    /**
     * 订单详情
     *
     * @param openid
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId) {
        OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId);
        return ResultUtil.success(orderDTO);
    }

    /**
     * 取消订单
     *
     * @param openid
     * @param orderId
     * @return
     */
    @GetMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId) {
        buyerService.cancelOrder(openid, orderId);
        return ResultUtil.success(ResultEnum.SUCCESS);
    }
}
